window.onload = function () {
    /**
     * 42. 接雨水
     */
    const test42 = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
    console.log(`${test42} => ${trap(test42)}`)
    document.getElementById("problem-42").innerHTML = `${test42} => ${trap(test42)}`

    /**
     * 26. 删除有序数组中的重复项
     */
    const test26 = [1, 1, 2]
    console.log(`${test26} => ${removeDuplicates(test26)}`)
    document.getElementById("problem-26").innerHTML = `${test26} => ${removeDuplicates(test26)}`

    /**
     * 189. 旋转数组
     */
    const test189 = [1, 2, 3, 4, 5, 6, 7]
    const test189_k = 3
    console.log(`${test189}, ${test189_k} => ${rotate(test189, test189_k)}`)
    document.getElementById("problem-189").innerHTML = `${test189}, ${test189_k} => ${rotate(test189, test189_k)}`
}

/**
 * 42. 接雨水
 * @param {number[]} height
 * @return {number}
 */
var trap = function (height) {
    let ans = 0;
    let left = 0, right = height.length - 1;
    let leftMax = 0, rightMax = 0;
    while (left < right) {
        leftMax = Math.max(leftMax, height[left]);
        rightMax = Math.max(rightMax, height[right]);
        if (height[left] < height[right]) {
            ans += leftMax - height[left];
            ++left;
        } else {
            ans += rightMax - height[right];
            --right;
        }
    }
    return ans;
};

/**
 * 26. 删除有序数组中的重复项
 * @param {number[]} nums
 * @return {number}
 */
var removeDuplicates = function (nums) {
    let i = 0, j = 0;
    while (j < nums.length) {
        while (nums[j] === nums[j + 1]) j++;
        if (j > i) {
            nums.splice(i, j - i);
            j = i;
        } else {
            j++;
            i++;
        }
    }
    return nums.length;
};

/**
 * 189. 旋转数组
 * @param {number[]} nums
 * @param {number} k
 * @return {void} Do not return anything, modify nums in-place instead.
 */
var rotate = function (nums, k) {
    nums.splice(0, 0, ...nums.splice(-(k %= nums.length), k))
    return nums
};
